<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">添加用户</div>
      <el-form ref="form" :model="form" label-width="130px">
        <el-form-item label="昵称" maxlength="20">
          <el-input v-model="form['nickname']"/>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form['gender']">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form['status']" placeholder="请选择">
            <el-option :value="1" label="启用"/>
            <el-option :value="2" label="冻结"/>
            <el-option :value="3" label="注销"/>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="form['mobile']"/>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form['email']"/>
        </el-form-item>
        <el-form-item label="联系地址">
          <el-input v-model="form['address']"/>
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input v-model="form['remark']" type="textarea" rows="3" maxlength="50"/>
        </el-form-item>
        <el-form-item label="头像">
          <el-select v-model="form['avatar']" placeholder="请选择">
            <el-option
              v-for="item in avatarList"
              :key="item.id"
              :label="item.label"
              :value="item.id">
              <img :src="item['url']" style="float: left;height:30px;margin-top: 2px;">
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item['label'] }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户身份">
          <el-radio-group v-model="form['userType']">
            <el-radio :label="0">普通用户</el-radio>
            <el-radio :label="1">司机</el-radio>
            <el-radio :label="2">核销员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'UserAdd',
    data() {
      return {
        loading: false,
        // 默认头像列表
        avatarList: [],
        form: {
          // 昵称
          nickname: '',
          //  性别
          gender: 1,
          // 状态
          status: 1,
          // 手机
          mobile: '',
          // 邮箱
          email: '',
          // 地址
          address: '',
          // 备注信息
          remark: '',
          // 头像
          avatar: '',
          // 用户身份
          userType: 0
        }
      }
    },
    mounted(){
      this.loadAvatarList();
    },
    methods: {
      // 加载默认头像列表
      loadAvatarList(){
        (async () => {
          const result = await this.$service.applicationGetDefaultAvatar();
          if (result.code === 0) {
            this.avatarList = result.data.avatarList || [];
          }
        })();
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            (async () => {
              const params = Object.assign({}, this.form);
              const result = await this.$service.applicationAddCustomer(params);
              this.loading = false;
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.$router.back();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>
